package com.yao.sample.ui;

import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.Image;
import java.awt.Point;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.MouseEvent;

import javax.swing.AbstractAction;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTabbedPane;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;
import javax.swing.border.EtchedBorder;
import javax.swing.event.MouseInputAdapter;
import javax.ws.rs.core.MediaType;

import org.apache.cxf.jaxrs.client.WebClient;
import org.jb2011.lnf.beautyeye.BeautyEyeLNFHelper;

import com.yao.sample.vo.Word;

public class MainFrame extends JFrame{
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 14514978687300349L;

	public MainFrame() {
		int screenWidth = Toolkit.getDefaultToolkit().getScreenSize().width;
		int screenHeight = Toolkit.getDefaultToolkit().getScreenSize().height;
		setBounds((screenWidth-1000)/2, (screenHeight-760)/2, 1000, 760);
		setTitle("DictionaryYao");
		setDefaultCloseOperation(EXIT_ON_CLOSE);
		initFrame();
	}
	
	public static void main(String[] args) {
		try {
			BeautyEyeLNFHelper.launchBeautyEyeLNF();
		} catch (Exception e) {
			e.printStackTrace();
		}
//		try {
//
//            UIManager.setLookAndFeel("com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel");
//
//        } catch (UnsupportedLookAndFeelException e) {
//
//            // handle exception
//
//        } catch (ClassNotFoundException e) {
//
//            // handle exception
//
//        } catch (InstantiationException e) {
//
//            // handle exception
//
//        } catch (IllegalAccessException e) {
//
//            // handle exception
//
//        }

		SwingUtilities.invokeLater(new Runnable() {
			
			@Override
			public void run() {
				createGUIAndShow();
			}

		});
	}

	private static void createGUIAndShow() {
		new MainFrame();
	}
	
	public void initFrame() {
		JPanel panel = new JPanel();
		JPanel contentPanel = new JPanel();
		JPanel meansPanel = new JPanel();
		meansPanel.setLayout(new GridLayout(3,1,0,5));
		JScrollPane scrollPane = new JScrollPane(contentPanel);
		contentPanel.setBorder(new EtchedBorder(5));
		contentPanel.setLayout(new BorderLayout());
		JTabbedPane simpleMean = new JTabbedPane();
		JTabbedPane otherMeans = new JTabbedPane();
		JTabbedPane examples = new JTabbedPane();
		JLabel wordTitle = new JLabel();
		wordTitle.setText("<html>" +
				"<p>" +
				"<span style='font-size:30px'>book</span>" +
				"&nbsp;&nbsp;&nbsp;<span style='color:green;font-size:14px;'>[英][bʊk]</span>"+
				"&nbsp;<span style='color:green;font-size:14px;'>[美][bʊk]</span>"+
				"</p>"+
				"<br/>"+
				"</html>");
		JLabel simpleMeanLabel = new JLabel();
		simpleMeanLabel.setText("<html>" +
				"<p style='font-size: 12px'>n.书；卷；课本；账簿</p>" +
				"<p style='font-size: 12px'>vt.& vi.预订</p>" +
				"<p style='font-size: 12px'>登记；（向旅馆、饭店、戏院等）预约；立案（控告某人）；订立演出契约</p>" +
				"<p style='font-size: 12px'>adj.书的；账簿上的；得之（或来自）书本的；按照（或依据）书本的</p>" +
				"</html>");
		simpleMean.add("简明释义", simpleMeanLabel);
		
		JLabel otherMeanLabel1 = new JLabel();
		otherMeanLabel1.setText("<html>" +
				"<p style='font-size: 12px'>n.书；卷；课本；账簿</p>" +
				"<p style='font-size: 12px'>vt.& vi.预订</p>" +
				"<p style='font-size: 12px'>登记；（向旅馆、饭店、戏院等）预约；立案（控告某人）；订立演出契约</p>" +
				"<p style='font-size: 12px'>adj.书的；账簿上的；得之（或来自）书本的；按照（或依据）书本的</p>" +
				"</html>");
		JLabel otherMeanLabel2 = new JLabel();
		otherMeanLabel2.setText("<html>" +
				"<p style='font-size: 12px'>n.书；卷；课本；账簿</p>" +
				"<p style='font-size: 12px'>vt.& vi.预订</p>" +
				"<p style='font-size: 12px'>登记；（向旅馆、饭店、戏院等）预约；立案（控告某人）；订立演出契约</p>" +
				"<p style='font-size: 12px'>adj.书的；账簿上的；得之（或来自）书本的；按照（或依据）书本的</p>" +
				"</html>");
		JLabel otherMeanLabel3 = new JLabel();
		otherMeanLabel3.setText("<html>" +
				"<p style='font-size: 12px'>n.书；卷；课本；账簿</p>" +
				"<p style='font-size: 12px'>vt.& vi.预订</p>" +
				"<p style='font-size: 12px'>登记；（向旅馆、饭店、戏院等）预约；立案（控告某人）；订立演出契约</p>" +
				"<p style='font-size: 12px'>adj.书的；账簿上的；得之（或来自）书本的；按照（或依据）书本的</p>" +
				"</html>");
		JLabel otherMeanLabel4 = new JLabel();
		otherMeanLabel4.setText("<html>" +
				"<p style='font-size: 12px'>n.书；卷；课本；账簿</p>" +
				"<p style='font-size: 12px'>vt.& vi.预订</p>" +
				"<p style='font-size: 12px'>登记；（向旅馆、饭店、戏院等）预约；立案（控告某人）；订立演出契约</p>" +
				"<p style='font-size: 12px'>adj.书的；账簿上的；得之（或来自）书本的；按照（或依据）书本的</p>" +
				"</html>");
		otherMeans.add("格林斯高阶英汉词典", otherMeanLabel1);
		otherMeans.add("百科释义", otherMeanLabel2);
		otherMeans.add("短语词组", otherMeanLabel3);
		otherMeans.add("同反义词", otherMeanLabel4);
		
		JLabel exampleLabel = new JLabel();
		exampleLabel.setText("<html>" +
				"<ol style='font-size: 12px'>" +
				"<li>" +
				"<p>You read the book.</p>"+
				"<p>你读读这本书。</p>"+
				"</li>"+
				"<li>" +
				"<p>Cannot open the phone book file.</p>"+
				"<p>无法打开电话簿文件。</p>"+
				"</li>"+
				"<li>" +
				"<p>Time alone with a good book.</p>"+
				"<p>一个人静静地看一本好书。</p>"+
				"</li>"+
				"<li>" +
				"<p>An ebook is a book in electronic format.</p>"+
				"<p>电子书就是电子格式的书籍。</p>"+
				"</li>"+
				"<li>" +
				"<p>The book deals with asian problems.</p>"+
				"<p>这书本论述了亚洲问题。</p>"+
				"</li>"+
				"</ol>" +
				"</html>");
		examples.add("例句", exampleLabel);
		
		contentPanel.add(wordTitle,BorderLayout.NORTH);
		meansPanel.add(simpleMean);
		meansPanel.add(otherMeans);
		meansPanel.add(examples);
		contentPanel.add(meansPanel,BorderLayout.CENTER);
		JPanel headerPanel = new JPanel();
		headerPanel.setBackground(null);
		headerPanel.setLayout(new GridLayout(2, 1, 0, 5));
		JPanel logoPanel = new JPanel();
		logoPanel.setBackground(null);
		JPanel searchPanel = new JPanel();
		searchPanel.setBackground(null);
		JButton button = new JButton("查  询");
		button.setToolTipText("查询单词");
		button.setFont(new Font("微软雅黑", Font.BOLD, 16));
		JTextField textField = new JTextField(30);
		textField.setFont(new Font("微软雅黑", Font.PLAIN, 20));
		textField.addKeyListener(new KeyConfirmAction(button));
		searchPanel.add(textField);
		searchPanel.add(button);
		Image image = Toolkit.getDefaultToolkit().getImage(this.getClass().getResource("/images/logo.png"));
		JLabel logoLabel = new JLabel(new ImageIcon(image));
		logoPanel.add(logoLabel);
		headerPanel.add(logoPanel);
		headerPanel.add(searchPanel);
		panel.add(headerPanel);
		setLayout(new BorderLayout());
		add(panel,BorderLayout.NORTH);
		add(scrollPane,BorderLayout.CENTER);
		button.addActionListener(new SearchWordAction(textField));
		DraggableAction draggableAction = new DraggableAction();
		addMouseListener(draggableAction);
		addMouseMotionListener(draggableAction);
		setVisible(true);
	}
	
	class SearchWordAction extends AbstractAction {
		/**
		 * 
		 */
		private static final long serialVersionUID = 7235549499232856476L;
		
		private JTextField textField;
		public SearchWordAction(JTextField textField) {
			this.textField = textField;
		}
		
		@Override
		public void actionPerformed(ActionEvent e) {
			if(textField.getText().isEmpty()) return;
			final Word word = WebClient.create("http://localhost:80").path("/DictionaryYao/word/detail/"+textField.getText()).accept(MediaType.TEXT_XML).get(Word.class);
			if(word==null){
				JOptionPane.showMessageDialog(textField.getRootPane(), "对不起，没有查到此单词！", "Sorry", JOptionPane.ERROR_MESSAGE);
			} else {
				JOptionPane.showMessageDialog(textField.getRootPane(), word.getSpelling(),"",JOptionPane.INFORMATION_MESSAGE);
			}
		}
		
	}
	
	class KeyConfirmAction extends KeyAdapter {
		private JButton confirmButton;

		public KeyConfirmAction(JButton confirmButton) {
			this.confirmButton = confirmButton;
		}
		
		@Override
		public void keyPressed(KeyEvent e) {
			if(e.getKeyCode()==KeyEvent.VK_ENTER) {
				confirmButton.doClick();
			}
		}
	}
	
	class DraggableAction extends MouseInputAdapter {
		private Point original;
		private Container source;
		
		@Override
		public void mouseDragged(MouseEvent e) {
			source = (Container) e.getSource();
			Point p = new Point(source.getX()+(e.getX()-(int)original.getX()), source.getY()+(e.getY()-(int)original.getY()));
			source.setLocation(p);
		}
		@Override
		public void mousePressed(MouseEvent e) {
			original = new Point(e.getX(), e.getY());
		}
	}
}
